package nl.gamayun.whohasit.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import nl.gamayun.whohasit.R;
import nl.gamayun.whohasit.WhoHasIt;
import nl.gamayun.whohasit.WhoHasItHandler;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.util.Log;


public class WhoHasItUtilsTest  extends AndroidTestCase {

	static final private String	TAG	= WhoHasItUtilsTest.class.getSimpleName();

	private Resources resources;
	private SharedPreferences sharedPrefs;
	private SimpleDateFormat dateformat; 

	@Override
	protected void setUp() throws Exception
	{
		resources = WhoHasIt.getContext().getResources();
		sharedPrefs = PreferenceManager.getDefaultSharedPreferences(WhoHasIt.getContext());
		dateformat = new SimpleDateFormat("yyyyMMdd"); 
		super.setUp();
	}
	
	
	public void testGetWhoHasItServiceWithContext()
	{
		WhoHasItHandler whoHasItService = WhoHasItUtils.getWhoHasItHandler(WhoHasIt.getContext());
		assertTrue("No instance of WhoHasItService", whoHasItService != null);
	}
	

	public void testGetDefaultReturnDate()
	{
		Editor editor = sharedPrefs.edit();
		assertTrue(resources.getString(R.string.preferences_return_key)+" does not exist in preferences!", 
				sharedPrefs.contains(resources.getString(R.string.preferences_return_key)));
		editor.putString(resources.getString(R.string.preferences_return_key), "1");
		editor.commit();
		Calendar test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("1 week is wrong",testDate(test, Calendar.WEEK_OF_YEAR, 1));
		editor.putString(resources.getString(R.string.preferences_return_key), "2");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("2 weeks is wrong",testDate(test, Calendar.WEEK_OF_YEAR, 2));
		editor.putString(resources.getString(R.string.preferences_return_key), "3");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("3 weeks is wrong",testDate(test, Calendar.WEEK_OF_YEAR, 3));
		editor.putString(resources.getString(R.string.preferences_return_key), "4");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("1 month is wrong",testDate(test, Calendar.MONTH, 1));
		editor.putString(resources.getString(R.string.preferences_return_key), "5");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("2 months is wrong",testDate(test, Calendar.MONTH, 2));
		editor.putString(resources.getString(R.string.preferences_return_key), "6");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("3 months is wrong",testDate(test, Calendar.MONTH, 3));
		editor.putString(resources.getString(R.string.preferences_return_key), "7");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("6 months is wrong",testDate(test, Calendar.MONTH, 6));
		editor.putString(resources.getString(R.string.preferences_return_key), "8");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("1 year is wrong",testDate(test, Calendar.YEAR, 1));
		editor.putString(resources.getString(R.string.preferences_return_key), "9");
		editor.commit();
		test = WhoHasItUtils.getDefaultReturnDate();
		assertTrue("default 1 month is wrong",testDate(test, Calendar.MONTH, 1));
	}
	
	private boolean testDate(Calendar test, int field, int value)
	{
		Calendar now = Calendar.getInstance();
		now.add(field, value);
		Log.d(TAG,"Now: "+dateformat.format(now.getTime()));
		Log.d(TAG,"Test: "+dateformat.format(test.getTime()));
		return dateformat.format(now.getTime()).equals(dateformat.format(test.getTime()));
	}
}